import VueRouter from "vue-router";
import Vue from "vue";
Vue.use(VueRouter);

import Home from "./views/HomePage";//首页
import ClassificationPage from "./views/ClassificationPage";//分类页
import CartPage from "./views/CartPage";//购物车页
import AboutPage from "./views/AboutPage";//关于我页面
import DetailPage from "./views/DetailPage";//物品详情页面
import SearchPage from "./views/SearchPage";//搜索物品详情页面
import LoginPage from "./views/LoginPage";//登录页面
// import SearchComponentPage from "./views/SearchComponentPage";//点击进入搜索页面


// import TestVuex from "./testVuex/test";//练习vuex页面

const config={
    mode:"hash",
    routes:[
        // {//练习vuex页面
        //     path:"/",
        //     component:TestVuex
        // },
        {
            path:"/",
            redirect:"/home"
        },
        {
            path:"/home",
            component:Home,
            meta:{
                navBarName:"SearchBar",//不区分大小写(貌似)
                bottomBarName:"BottomBar",
            }
        },
        {
            path:"/classify",
            component:ClassificationPage,
            meta:{
                navBarName:"SearchBar",
                bottomBarName:"BottomBar",
            }
        },
        {
            path:"/cart",
            component:CartPage,
            meta:{
                navBarName:"CartBar",
                bottomBarName:"CartBottomBar",
                fixedMenuShow:true
            }
        },
        {
            path:"/about",
            component:AboutPage,
            meta:{
                bottomBarName:"BottomBar"
            }
        },
        {
            path:"/detail",
            component:DetailPage,
            meta:{
                bottomBarName:"DetailBottomBar",
                fixedMenuShow:true
            }
        },
        {
            path:'/searchGood',
            component:SearchPage,
            meta:{
                navBarName:"SearchBarForSearchPage",
                fixedMenuShow:true
            }
        },
        {
            path:"/login",
            component:LoginPage
        },
        // {
        //     path:"/search",
        //     component:SearchComponentPage,
        //     meta:{
        //         navBarName:"SearchBarForSearchPage"
        //     }
        // }
    ]
}

const router=new VueRouter(config);


//定义全局守卫
const PrivatePath=["/cart","/about"];
router.beforeEach((to,from,next)=>{
    if(PrivatePath.indexOf(to.path)==-1){
        next();
        return;
    }else{
        let token=JSON.parse(sessionStorage.getItem("token"));
        if(token){
            next();
        }else{
            next("/login");
        }
    }
})
export default router;
